package com.lc.pianostudent.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostCustomerIndex;
import com.lc.pianostudent.conn.PostGetCode;
import com.lc.pianostudent.conn.PostRegister;
import com.lc.pianostudent.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilMatches;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.AppGetVerification;
import com.zcx.helper.view.AppPassword;
import com.zcx.helper.view.AppUsername;
import com.zcx.helper.view.AppVerification;

/**
 * Created by Administrator on 2018/6/21.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(value = R.id.register_next,isClick = true) private LinearLayout register_next;
    @BoundView(R.id.register_username) private AppUsername register_username;
    @BoundView(R.id.register_verification) private AppVerification register_verification;
    @BoundView(R.id.register_password) private AppPassword register_password;
    @BoundView(R.id.register_confirm_password) private AppPassword register_confirm_password;
    @BoundView(value = R.id.register_verification_get,isClick = true) private AppGetVerification register_verification_get;  //获取验证码
    private   PostGetCode.PostGetCodeInfo currentInfo;
    //发送验证码接口
    public PostGetCode postGetCode = new PostGetCode(new AsyCallBack<PostGetCode.PostGetCodeInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, PostGetCode.PostGetCodeInfo postGetCodeInfo) throws Exception {
            countTime.start();
            currentInfo = postGetCodeInfo;
            UtilToast.show(toast);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    //注册接口
    public PostRegister postRegister = new PostRegister(new AsyCallBack<PostRegister.RegisterInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, PostRegister.RegisterInfo registerInfo) throws Exception {
            super.onSuccess(toast, type, o, registerInfo);
            startActivity(new Intent(RegisterActivity.this,RegisterNextActivity.class).putExtra("telephone",register_username.getText().toString().trim())
                    .putExtra("password",register_password.getText().toString().trim()));
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });

    //获取客服电话
    private PostCustomerIndex postCustomerIndex = new PostCustomerIndex(new AsyCallBack<PostCustomerIndex.PostCustomerIndexInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostCustomerIndex.PostCustomerIndexInfo postCustomerIndexInfo) throws Exception {
            BaseApplication.BasePreferences.saveKefu(postCustomerIndexInfo.tel);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        setBackTrue();
        initView();
    }

    private void initView(){
        setTitleName("注册");
        postCustomerIndex.execute();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.register_next:

                String phoneText = register_username.getText().toString().trim();
                String codeText = register_verification.getText().toString().trim();
                String pwd = register_password.getText().toString().trim();
                String confirm_pwd = register_confirm_password.getText().toString().trim();

                if (TextUtils.isEmpty(phoneText)) {
                    UtilToast.show("请输入手机号");
                } else if (!UtilMatches.checkMobile(phoneText)) {
                    UtilToast.show("请输入正确手机号");
                } else if (TextUtils.isEmpty(codeText) ){
                    UtilToast.show("请输入验证码");
                }else if (TextUtils.isEmpty(pwd)) {
                    UtilToast.show("请输入密码");
                }  else if (TextUtils.isEmpty(confirm_pwd)) {
                    UtilToast.show("请输入确认密码");
                } else if (!Validator.isPassword2(pwd)) {
                    UtilToast.show("请输入6-12位数字、英文");
                }else if (!Validator.isPassword2(confirm_pwd)) {
                    UtilToast.show("请输入6-12位数字、英文");
                }else if (!pwd.equals(confirm_pwd)) {
                    UtilToast.show("两次输入的密码不一致");
                }  else {
                    postRegister.username = phoneText;
                    postRegister.sms_code = codeText;
                    postRegister.password = pwd;
                    postRegister.confirm_password = confirm_pwd;
                    postRegister.type = "0";
                    postRegister.execute();
                }

                break;
            case R.id.register_verification_get:

                if (TextUtils.isEmpty(register_username.getText().toString().trim())) {
                    UtilToast.show("请输入手机号");
                } else if (!UtilMatches.checkMobile(register_username.getText().toString().trim())) {
                    UtilToast.show("请输入正确手机号");
                } else {


                    postGetCode.username = register_username.getText().toString().trim();
                    postGetCode.execute();
                }

                break;

        }
    }

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {
                ScaleScreenHelperFactory.getInstance().loadViewSize(register_verification_get, 24);
                register_verification_get.setText(millisUntilFinished / 1000 + "s后重新获取");
                register_verification_get.setEnabled(false);
            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                ScaleScreenHelperFactory.getInstance().loadViewSize(register_verification_get, 24);
                register_verification_get.setText("重新获取验证码");
                register_verification_get.setEnabled(true);
            }
        }
    };
}
